package com.jcfk.eam.service.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.context.ApplicationContextHolder;
import com.jcfk.common.security.constant.SecurityConstants;
import com.jcfk.common.security.service.TokenService;
import com.jcfk.common.web.domain.LoginInfo;
import com.jcfk.eam.dao.main.EamAssetInfoDao;
import com.jcfk.eam.domain.po.main.EamAssetInfo;
import com.jcfk.system.api.ifeign.SysMessageCenterServiceFeign;
import com.jcfk.system.domain.model.SendMsgModel;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.*;


/**
 * 资产台账
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-07-28
 */
@Service
public class EamAssetInfoJobService extends BaseService<EamAssetInfoDao, EamAssetInfo> {

    @Autowired
    private SysMessageCenterServiceFeign sysMessageCenterServiceFeign;

    /**
     * tokenService
     */
    @Autowired
    private TokenService tokenService;

    @Value("${xxl.job.executor.userId}")
    private String userId;

    @Value("${xxl.job.executor.userName}")
    private String userName;

    @Value("${xxl.job.executor.loginUserName}")
    private String loginUserName;

    @Value("${xxl.job.executor.empNo}")
    private String empNo;


    @XxlJob("promptEamAssetExpiration")
    public void promptEamAssetExpiration() {
        // 构造token
        getPersonLogin();

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取下个月的年份和月份
        YearMonth nextMonth = YearMonth.from(currentDate).plusMonths(1);
        // 下个月的第一天
        LocalDateTime firstDayOfNextMonth = nextMonth.atDay(1).atStartOfDay();
        // 下个月的最后一天（当前月的最后一天即为下个月的最后一天）
        LocalDateTime lastDayOfNextMonth = nextMonth.atEndOfMonth().atTime(23, 59, 59);


        QueryWrapper<EamAssetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().le(EamAssetInfo::getNextCheckDate, lastDayOfNextMonth);
        queryWrapper.lambda().ge(EamAssetInfo::getNextCheckDate, firstDayOfNextMonth);

        List<EamAssetInfo> eamAssetInfos = this.list(queryWrapper);

        for (EamAssetInfo eamAssetInfo : eamAssetInfos) {
            Map<String, Object> stringObjectMap = new HashMap<>();
            stringObjectMap.put("assetCode", eamAssetInfo.getAssetCode());
            stringObjectMap.put("month", nextMonth.getYear() + "-" + nextMonth.getMonth().getValue());
            SendMsgModel sendMsgModel = new SendMsgModel();
            List<String> msgType = new ArrayList<>();
            msgType.add("eam_prompt_expiration");
            sendMsgModel.setMsgTypeCodeList(msgType);
            sendMsgModel.setContent(stringObjectMap);
            sendMsgModel.setTitle("标定到期提示");

            List<String> userids = new ArrayList<>();
            userids.add(eamAssetInfo.getUsingPersonId());
            sendMsgModel.setReceiveUserIds(userids);
            sysMessageCenterServiceFeign.send(sendMsgModel);
        }

    }


    /**
     * 构造服务间调用token
     *
     * @return
     */
    public void getPersonLogin() {
        // 设置登录信息
        LoginInfo loginUser = new LoginInfo();
        loginUser.setUserId(userId);
        loginUser.setLoginName(userName);
        loginUser.setUserName(loginUserName);
        loginUser.setLoginTime(new Date());
        loginUser.getParameters().put("empNo", empNo);
        String token = tokenService.createLogin(loginUser);
        ApplicationContextHolder.set(SecurityConstants.AUTHORIZATION_KEY, token);
    }
}

